﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using HRM.Controller;
using System.Data;

namespace HRMServiceWebHost
{
    /// <summary>
    /// Summary description for TraCuu
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class TraCuu : System.Web.Services.WebService
    {

        //[WebMethod]
        //[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        //public void QuaTrinh(string sMaDonVi, string sSoSoBHXH, string sMaTheBHYT, string soCMT)
        //{

        //    JavaScriptSerializer js = new JavaScriptSerializer();
        //    Context.Response.Clear();
        //    Context.Response.ContentType = "application/json";
        //    //
        //    List<QuaTrinhBHXH> lst = new List<QuaTrinhBHXH>();
        //    QuaTrinhBHXH qt = null;
        //    DataTable dt = Premium_Controller.Instance().Premium_QTBH(sMaDonVi, sSoSoBHXH, 0, Null.MIN_DATE, DateTime.MaxValue, 0);

        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        qt = new QuaTrinhBHXH();

        //        qt.MaDonVi = Globals.Object_GetValue<string>(dr["Premium_WorkUnit"]);
        //        qt.TenDonVi = Globals.Object_GetValue<string>(dr["Premium_WorkName"]);
        //        qt.TuThang = Globals.Object_GetValue<DateTime>(dr["Premium_FromDate"]);
        //        qt.DenThang = Globals.Object_GetValue<DateTime>(dr["Premium_ToDate_Chuan"]);
        //        qt.ChucDanh = Globals.Object_GetValue<string>(dr["Premium_Job"]);
        //        qt.Loai = Globals.Object_GetValue<string>(dr["loaips"]);
        //        qt.PhuongAn = Globals.Object_GetValue<string>(dr["Detail_InsTypeCode"]);
        //        qt.MucLuong = Globals.Object_GetValue<decimal>(dr["Premium_Salary"]);
        //        qt.HeSoLuong = Globals.Object_GetValue<decimal>(dr["Premium_BasicFactor"]);
        //        qt.HeSoCV = Globals.Object_GetValue<decimal>(dr["Premium_PositionFactor"]);
        //        qt.TNN = Globals.Object_GetValue<decimal>(dr["TNNghe_Percent"]);
        //        qt.TNVK = Globals.Object_GetValue<decimal>(dr["TNVK_Percent"]);
        //        qt.SoThang = Globals.Object_GetValue<int>(dr["Premium_MonthNo"]);

        //        //
        //        lst.Add(qt);
        //    }
        //    Context.Response.Write(js.Serialize(lst));
        //}

        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public void QuaTrinh(string sSoSoBHXH, string sMaTheBHYT,string sMaDonVi, string sHoVaTen, string sNgaySinh)
        {

            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";
            //
            QuaTrinhModel model = new QuaTrinhModel();
            List<QuaTrinhBHXH> lst = new List<QuaTrinhBHXH>();
            QuaTrinhBHXH qt = null;
            DataTable dt = new DataTable();
            DateTime dNgaySinh = Null.MIN_DATE;
            DateTime.TryParseExact(sNgaySinh,"dd/MM/yyyy",null,System.Globalization.DateTimeStyles.None, out dNgaySinh);
            if (dNgaySinh < Null.MIN_DATE)
                dNgaySinh = Null.MIN_DATE;
            //
            DataSet ds = TraCuu_Controller.Instance().TraCuu_QTBH(sSoSoBHXH, sMaTheBHYT,sMaDonVi, sHoVaTen, dNgaySinh);
            if (ds.Tables.Count > 1)
            {
                dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    model.HoVaTen = Globals.Object_GetValue<string>(dt.Rows[0]["Employee_LastName"]) + " " + Globals.Object_GetValue<string>(dt.Rows[0]["Employee_FirstName"]);
                    model.NgaySinh = Globals.Object_GetValue<DateTime>(dt.Rows[0]["Employee_DateOfBirth"]);
                    model.CMT = Globals.Object_GetValue<string>(dt.Rows[0]["Employee_IDCardNo"]);
                    model.MaTheBHYT = Globals.Object_GetValue<string>(dt.Rows[0]["Employee_MedicalNo"]);
                    model.SoSoBHXH = Globals.Object_GetValue<string>(dt.Rows[0]["Employee_SiBookNo"]);
                }
            }
            //Lấy thông tin cơ bản, họ tên, ngày sinh, số thẻ, số sổ

            dt = ds.Tables[1];
            //Lấy thông tin quá trình bhxh
            foreach (DataRow dr in dt.Rows)
            {
                qt = new QuaTrinhBHXH();

                qt.MaDonVi = Globals.Object_GetValue<string>(dr["Premium_WorkUnit"]);
                qt.TenDonVi = Globals.Object_GetValue<string>(dr["Premium_WorkName"]);
                qt.TuThang = Globals.Object_GetValue<DateTime>(dr["Premium_FromDate"]);
                qt.DenThang = Globals.Object_GetValue<DateTime>(dr["Premium_ToDate_Chuan"]);
                qt.ChucDanh = Globals.Object_GetValue<string>(dr["Premium_Job"]);
                qt.Loai = Globals.Object_GetValue<string>(dr["loaips"]);
                qt.PhuongAn = Globals.Object_GetValue<string>(dr["Detail_InsTypeCode"]);
                qt.MucLuong = Globals.Object_GetValue<decimal>(dr["Premium_Salary"]);
                qt.HeSoLuong = Globals.Object_GetValue<decimal>(dr["Premium_BasicFactor"]);
                qt.HeSoCV = Globals.Object_GetValue<decimal>(dr["Premium_PositionFactor"]);
                qt.TNN = Globals.Object_GetValue<decimal>(dr["TNNghe_Percent"]);
                qt.TNVK = Globals.Object_GetValue<decimal>(dr["TNVK_Percent"]);
                qt.SoThang = Globals.Object_GetValue<int>(dr["Premium_MonthNo"]);

                //
                lst.Add(qt);
            }
            model.lstQuaTrinh = lst;
            Context.Response.Write(js.Serialize(model));
        }
    }

    public class QuaTrinhBHXH
    {
        public string MaDonVi { get; set; }
        public string TenDonVi { get; set; }
        public DateTime TuThang { get; set; }
        public DateTime DenThang { get; set; }
        public string ChucDanh { get; set; }
        public string Loai { get; set; }
        public string PhuongAn { get; set; }
        public decimal MucLuong { get; set; }
        public decimal HeSoLuong { get; set; }
        public decimal HeSoCV { get; set; }
        public decimal TNN { get; set; }
        public decimal TNVK { get; set; }
        public int SoThang { get; set; }
    }

    public class QuaTrinhModel
    {
        public string HoVaTen { get; set; }
        public string CMT { get; set; }
        public DateTime NgaySinh { get; set; }
        public string MaTheBHYT { get; set; }
        public string SoSoBHXH { get; set; }
        public List<QuaTrinhBHXH> lstQuaTrinh { get; set; }
    }
}
